//
//  LGNewBodyDataView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/6/25.
//  最新体征数据

#import "LGNewBodyDataView.h"
#import "LGSelectButtonItemView.h"
#import "DatePickerTwoView.h"
@interface LGNewBodyDataView()<LGSelectButtonItemDelegate,DatePickerViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UIImageView *iconView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIView *segmentView;
/**   */
@property (nonatomic , strong)UILabel *typeLabel;
/**   */
@property (nonatomic , strong)LGSelectButtonItemView *typeView;
/**   */
@property (nonatomic , strong)UILabel *weightLabel;
/**   */
@property (nonatomic , strong)UIButton *weightDate;
/**   */
@property (nonatomic , strong)UITextField *heightTF;
/**   */
@property (nonatomic , strong)UITextField *weightTF;
/**   */
@property (nonatomic , strong)UILabel *pressureLabel;
/**   */
@property (nonatomic , strong)UILabel *heartLabel;
/**   */
@property (nonatomic , strong)UILabel *heartDate;
/**   */
@property (nonatomic , strong)UIButton *heartButton;

@end

@implementation LGNewBodyDataView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

-(void)setTypeArray:(NSArray *)typeArray{
    _typeArray = typeArray;
    self.typeView.itemArray = typeArray;
}

-(void)setDiseaseArray:(NSArray *)diseaseArray{
    _diseaseArray = diseaseArray;
    self.typeView.resultArray = diseaseArray;
}


-(void)setBmiDic:(NSMutableDictionary *)bmiDic{
    _bmiDic = bmiDic;
    self.weightDate.selected = YES;
    [self.weightDate setTitle:bmiDic[@"bmi_date"] forState:UIControlStateNormal];
    self.heightTF.text = LGNSString(bmiDic[@"height"]);
    self.weightTF.text = LGNSString(bmiDic[@"weight"]);
}


-(void)setHeartDic:(NSDictionary *)heartDic{
    _heartDic = heartDic;
    NSString *dateString = LGNSString(heartDic[@"nyhadate"]);
    self.heartDate.text = dateString;
    self.heartButton.hidden = (dateString.length>0)?NO:YES;
    [self.heartButton setTitle:heartDic[@"nyharesult"] forState:UIControlStateNormal];
    [self.heartButton setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:5];
}

-(void)selectItemViewHeight:(CGFloat)height tag:(NSInteger)tag{
//    [self.pressureView ]
}

-(void)selectItemResultArray:(NSArray *)result tag:(NSInteger)tag{
    self.diseaseArray = result;
}

//身体体重日期
-(void)selectWeightDateAction{
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.currentDate = self.weightDate.titleLabel.text;
    pickerView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:pickerView];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    [self.bmiDic setObject:date forKey:@"bpdate"];
    [self.weightDate  setTitle:date forState:UIControlStateNormal];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    if(textField == self.heightTF){
        [self.bmiDic setObject:textField.text forKey:@"height"];
    }else{
        [self.bmiDic setObject:textField.text forKey:@"weight"];
    }
}

//心功能等级
-(void)heartButtonAction{
    
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.iconView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.segmentView];
    [self.baseView addSubview:self.typeLabel];
    [self.baseView addSubview:self.typeView];
    [self.baseView addSubview:self.weightLabel];
    [self.baseView addSubview:self.weightDate];
    [self.baseView addSubview:self.heightTF];
    [self.baseView addSubview:self.weightTF];
    [self.baseView addSubview:self.pressureLabel];
    [self.baseView addSubview:self.pressureView];
    [self.baseView addSubview:self.heartLabel];
    [self.baseView addSubview:self.heartDate];
    [self.baseView addSubview:self.heartButton];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(viewPix(5));
        make.left.equalTo(self).offset(viewPix(16));
        make.right.equalTo(self).offset(-viewPix(16));
        make.bottom.equalTo(self).offset(viewPix(15));
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.baseView).offset(viewPix(12));
        make.width.height.equalTo(@(viewPix(24)));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right).offset(viewPix(6));
        make.centerY.equalTo(self.iconView);
    }];
    [self.segmentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(48));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.height.equalTo(@(0.5));
    }];
    [self.heartDate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.heartLabel.mas_bottom).offset(viewPix(8));
        make.left.equalTo(self.baseView).offset(viewPix(16));
    }];
    [self.heartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.heartDate.mas_right).offset(viewPix(10));
        make.right.equalTo(self.baseView).offset(-viewPix(16));
        make.centerY.equalTo(self.heartDate);
        make.height.equalTo(@(viewPix(35)));
    }];
}
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:10];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}
-(UIImageView *)iconView{
    if(!_iconView){
        _iconView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"添加随访-最新体征数据"]];
    }
    return _iconView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"最新体征数据" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UIView *)segmentView{
    if(!_segmentView){
        _segmentView = [[UIView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(48), Screen_W-viewPix(56), 0.5)];
        _segmentView.backgroundColor = [UIColor colorWithString:@"#E8E8E8"];
    }
    return _segmentView;
}

-(UILabel *)typeLabel{
    if(!_typeLabel){
        _typeLabel = [UILabel lableWithText:@"共病类型(可多选)" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _typeLabel.frame = CGRectMake(viewPix(16), viewPix(58), viewPix(150), 21);
        _typeLabel.attributedText = [@"共病类型(可多选)" attributeString:@{NSFontAttributeName:LGFont(15)} range:NSMakeRange(4, 5)];
    }
    return _typeLabel;
}


-(LGSelectButtonItemView *)typeView{
    if(!_typeView){
        _typeView = [[LGSelectButtonItemView alloc] initWithFrame:CGRectMake(viewPix(12), kMaxY(self.typeLabel.frame)+viewPix(8), Screen_W-viewPix(56), viewPix(80))];
        _typeView.delegate = self;
        _typeView.type = @"multiple";
        _typeView.tag = 2000;
    }
    return _typeView;
}

-(UILabel *)weightLabel{
    if(!_weightLabel){
        _weightLabel = [UILabel lableWithText:@"身高体重" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _weightLabel.frame = CGRectMake(viewPix(16), kMaxY(self.typeView.frame)+viewPix(12), viewPix(150), 21);
    }
    return _weightLabel;
}

-(UIButton *)weightDate{
    if(!_weightDate){
        _weightDate = [UIButton buttonWithTitle:@"请选择..." titleFont:LGFont(14) normalColor:@"#BBBBBB" selectColor:@"#333333"];
        _weightDate.frame = CGRectMake(viewPix(12), kMaxY(self.weightLabel.frame)+viewPix(8), viewPix(123), viewPix(40));
        _weightDate.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _weightDate.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _weightDate.layer.borderWidth = 1;
        _weightDate.cornerRidus = 7;
        _weightDate.titleEdgeInsets = UIEdgeInsetsMake(0, 12, 0, 0);
        [_weightDate addTarget:self action:@selector(selectWeightDateAction) forControlEvents:UIControlEventTouchUpInside];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTipDown12"]];
        tipView.frame = CGRectMake(viewPix(123)-viewPix(24), viewPix(14), viewPix(12), viewPix(12));
        [_weightDate addSubview:tipView];
    }
    return _weightDate;
}

-(UITextField *)heightTF{
    if(!_heightTF){
        _heightTF = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.weightDate.frame)+viewPix(8), kMinY(self.weightDate.frame), viewPix(90), viewPix(40))];
        _heightTF.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _heightTF.tintColor = [UIColor colorWithString:@"#333333"];
        _heightTF.textColor = [UIColor colorWithString:@"#333333"];
        _heightTF.textAlignment = NSTextAlignmentCenter;
        _heightTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _heightTF.placeholder = @"身高...";
        _heightTF.layer.borderWidth = 1;
        _heightTF.cornerRidus = 7;
        _heightTF.font = LGFont(14);
        _heightTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 12, viewPix(40))];
        _heightTF.leftViewMode = UITextFieldViewModeAlways;
        _heightTF.rightViewMode = UITextFieldViewModeAlways;
        _heightTF.rightView = [self textFileRightView:@"cm"];
        [_heightTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _heightTF;
}

-(UITextField *)weightTF{
    if(!_weightTF){
        _weightTF = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.heightTF.frame)+viewPix(8), kMinY(self.weightDate.frame), viewPix(90), viewPix(40))];
        _weightTF.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _weightTF.tintColor = [UIColor colorWithString:@"#333333"];
        _weightTF.textColor = [UIColor colorWithString:@"#333333"];
        _weightTF.textAlignment = NSTextAlignmentCenter;
        _weightTF.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        _weightTF.placeholder = @"体重...";
        _weightTF.layer.borderWidth = 1;
        _weightTF.cornerRidus = 7;
        _weightTF.font = LGFont(14);
        _weightTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 12, viewPix(40))];
        _weightTF.leftViewMode = UITextFieldViewModeAlways;
        _weightTF.rightViewMode = UITextFieldViewModeAlways;
        _weightTF.rightView = [self textFileRightView:@"kg"];
        [_weightTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _weightTF;
}

-(UILabel *)pressureLabel{
    if(!_pressureLabel){
        _pressureLabel = [UILabel lableWithText:@"血压心率" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _pressureLabel.frame = CGRectMake(viewPix(16), kMaxY(self.weightDate.frame)+viewPix(16), viewPix(150), 21);
    }
    return _pressureLabel;
}

-(LGFollowPressureView *)pressureView{
    if(!_pressureView){
        _pressureView = [[LGFollowPressureView alloc] initWithFrame:CGRectMake(viewPix(12), kMaxY(self.pressureLabel.frame)+viewPix(8), Screen_W-viewPix(56), viewPix(68))];
        _pressureView.layer.borderColor = [UIColor colorWithString:@"#E0E0E0"].CGColor;
        _pressureView.layer.borderWidth = 1;
        _pressureView.cornerRidus = 7;
        _pressureView.canEdit = YES;
    }
    return _pressureView;
}

-(UILabel *)heartLabel{
    if(!_heartLabel){
        _heartLabel = [UILabel lableWithText:@"心功能等级" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _heartLabel.frame = CGRectMake(viewPix(16), kMaxY(self.pressureView.frame)+viewPix(16), viewPix(150), 21);
    }
    return _heartLabel;
}

-(UILabel *)heartDate{
    if(!_heartDate){
        _heartDate = [UILabel lableWithText:@"" colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _heartDate;
}

-(UIButton *)heartButton{
    if(!_heartButton){
        _heartButton = [UIButton buttonWithTitle:@"" titleFont:LGFontWeight(14, UIFontWeightMedium) textColor:@"#333333" imageName:@"grayTip12"];
        _heartButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_heartButton addTarget:self action:@selector(heartButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _heartButton;
}


-(UIView *)textFileRightView:(NSString *)content{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(26), viewPix(40))];
    if([content isEqualToString:@"tip"]){
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTipDown12"]];
        tipView.frame = CGRectMake(viewPix(7), viewPix(14), viewPix(12), viewPix(12));
        [baseView addSubview:tipView];
    }else{
        UILabel *unitLabel = [UILabel lableWithText:content colorString:@"#666666" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        unitLabel.frame = CGRectMake(0, 0, viewPix(26), viewPix(40));
        [baseView addSubview:unitLabel];
    }
    return baseView;
}


@end




